/**
 * Title:       APScriptHandler.java
 * Copyright:   Copyright (c) 1996-2004 The Agent Factory Working Group. All rights reserved.
 * Licence:     This file is free software; you can redistribute it and/or modify
 *              it under the terms of the GNU Lesser General Public License as published by
 *              the Free Software Foundation; either version 2.1, or (at your option)
 *              any later version.
 *
 *              This file is distributed in the hope that it will be useful,
 *              but WITHOUT ANY WARRANTY; without even the implied warranty of
 *              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *              GNU Lesser General Public License for more details.
 *
 *              You should have received a copy of the GNU Lesser General Public License
 *              along with Agent Factory; see the file COPYING.  If not, write to
 *              the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *              Boston, MA 02111-1307, USA.
 */
package com.agentfactory.platform.core;

import com.agentfactory.platform.mts.FIPAContent;
import com.agentfactory.platform.mts.Node;
import java.io.Serializable;
import java.util.ArrayList;

/**
 * This implements the FIPA Agent ID format specified in
 * 
 * @author  Rem Collier
 *
 * added to by Donal O'Kane 28/02/05 - for storing a description of the services provided by an agent
 */
public class AgentID implements Serializable {
    private String name;
    private ArrayList addresses;
    private ArrayList resolvers;
    private ArrayList servicedescriptions;
    
    private AgentID() {
        addresses = new ArrayList();
        resolvers = new ArrayList();
        servicedescriptions = new ArrayList();
    }
    
    /** Creates a new instance of AgentID */
    public AgentID(String name, ArrayList addresses, ArrayList resolvers) {
        this.name = name;
        this.addresses = addresses;
        this.resolvers = resolvers;
        this.servicedescriptions = new ArrayList();
    }
    
    public AgentID(String name, ArrayList addresses, ArrayList resolvers, ArrayList servicedescriptions) {
        this.name = name;
        this.addresses = addresses;
        this.resolvers = resolvers;
        this.servicedescriptions = servicedescriptions;
    }
    
    /**
     * Creates a new instance of AgentID based upon the FIPA content format.
	 * The FIPA content format is:
     * (agent-identifier :name rem :addresses (sequence addr1) :resolvers (sequence addr1))
     *
     * @param content the fipa content to be parsed
     */
    public AgentID(FIPAContent content) {
        this();
        FIPAContent argument = null;
        if (content.getCommand().equals("agent-identifier")) {
            for (int i = 0; i < content.size(); i++) {
                argument = content.get(i);
                if (argument.getCommand().equals(":name")) {
                    name = content.get(++i).toString();
                } else if (argument.getCommand().equals(":addresses")) {
                    argument = content.get(++i);
                    for (int j=0;j<argument.size();j++) {
                        addresses.add(argument.get(j).toString());
                    }
                } else if (argument.getCommand().equals(":resolvers")) {
                    argument = content.get(++i);
                    for (int j=0;j<argument.size();j++) {
                        resolvers.add(argument.get(j).toString());
                    }
                } else if (argument.getCommand().equals(":servicedescriptions")) {
                    argument = content.get(++i);
                    for (int j=0;j<argument.size();j++) {
                        servicedescriptions.add(argument.get(j).toString());
                    }
                }
            }
        }
    }
    
    /**
     * Creates a new instance of AgentID based upon the XML FIPA agent id format:
     *
     * @param node an internal representation of the XML structure to be parsed
     */
    public AgentID(Node node) {
        this();
        
        Node part = null;
        
        if (node.getName().equals("agent-identifier")) {
            for (int i = 0; i < node.numChildren(); i++) {
                part = (Node) node.getChild(i);
                if (part.getName().equals("name")) {
                    name = (String) part.getChild(0);
                } else if (part.getName().equals("addresses")) {
                    for (int j=0;j<part.numChildren();j++) {
                        addresses.add(((Node) part.getChild(j)).getChild(0).toString());
                    }
                } else if (part.getName().equals("resolvers")) {
                    for (int j=0;j<part.numChildren();j++) {
                        addresses.add(((Node) part.getChild(j)).getChild(0).toString());
                    }
                } else if (part.getName().equals("servicedescriptions")) {
                    for (int j=0;j<part.numChildren();j++) {
                        servicedescriptions.add(((Node) part.getChild(j)).getChild(0).toString());
                    }
                }
            }
        }
    }

    public String getName() {
        return name;
    }
    
    public ArrayList getAddresses() {
        return addresses;
    }
    
    public ArrayList getResolvers() {
        return resolvers;
    }
    
     public ArrayList getServiceDescriptionss() {
        return servicedescriptions;
    }
    
    public void toXMLString(StringBuffer buf, String offset) {
        buf.append(offset + "<agent-identifier>\n");
        buf.append(offset + "\t<name>" + name + "</name>\n");
        buf.append(offset + "\t<addresses>\n");
        for (int i = 0; i < addresses.size(); i++) {
            buf.append(offset + "\t\t<url>" + addresses.get(i) + "</url>\n");
        }
        buf.append(offset + "\t</addresses>\n");
        buf.append(offset + "</agent-identifier>\n");
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String toFIPAString() {
        StringBuffer buf = new StringBuffer();
        buf.
        append("(agent-identifier ").
        append(":name ").
        append(name).
        append(" :addresses ").
        append("(sequence");
        
        for (int i = 0; i < addresses.size(); i++) {
            buf.
            append(" ").
            append(addresses.get(i));
        }
        
        buf.append("))");
        
        return buf.toString();
    }
    
    public String toFOSString() {
        
        StringBuffer buf = new StringBuffer();
        buf.
        append("agentID(").
        append(name).
        append(",addresses(");
        
        boolean first = true;
        for (int i = 0; i < addresses.size(); i++) {
            if (first) {
                first = false;
            } else {
                buf.append(",");
            }
            
            buf.append(addresses.get(i));
        }
        
        buf.append("))");
        
        return buf.toString();
    }
    
    public String toString() {
        return toFOSString();
    }
    
    public Object clone() {
        ArrayList addr = new ArrayList();
        ArrayList res = new ArrayList();
        String address = null;
        for (int index=0; index < addresses.size(); index++) {
            addr.add(new String((String) addresses.get(index)));
        }
        
        for (int index=0; index < resolvers.size(); index++) {
            res.add(new String((String) resolvers.get(index)));
        }
        
        String nme = new String(name);
        return new AgentID(name, addr, res);
    }
}
